package com.winning.platwebservice.utils;

import org.dom4j.Element;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * xml的工具类集合
 */
public class XmlUtils {


    /**
     * 将XML节点内容转换成Map集合
     *
     * @param
     * @return
     */
    public static Map xml2Map(Element e) {
        Map map = new LinkedHashMap();
        List list = e.elements();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Element iter = (Element) list.get(i);
                List mapList = new ArrayList();
                if (iter.elements().size() > 0) {
                    Map m = xml2Map(iter);
                    if (map.get(iter.getName()) != null) {
                        Object obj = map.get(iter.getName());
                        if (!obj.getClass().getName().equals("java.util.ArrayList")) {
                            mapList = new ArrayList();
                            mapList.add(obj);
                            mapList.add(m);
                        }
                        if (obj.getClass().getName().equals("java.util.ArrayList")) {
                            mapList = (List) obj;
                            mapList.add(m);
                        }
                        map.put(iter.getName().toUpperCase(), mapList);
                    } else
                        map.put(iter.getName().toUpperCase(), m);
                } else {
                    if (map.get(iter.getName()) != null) {
                        Object obj = map.get(iter.getName());
                        if (!obj.getClass().getName().equals("java.util.ArrayList")) {
                            mapList = new ArrayList();
                            mapList.add(obj);
                            mapList.add(iter.getText());
                        }
                        if (obj.getClass().getName().equals("java.util.ArrayList")) {
                            mapList = (List) obj;
                            mapList.add(iter.getText());
                        }
                        map.put(iter.getName(), mapList);
                    } else {
                        map.put(iter.getName().toUpperCase(), iter.getText());
                    }
                }
            }
        } else {
                map.put(e.getName(), e.getText());
        }
        return map;
    }

    /***将Map转换成XML
     *
     * @param map
     * @param sb
     */
    private static void map2Element(Map<String, Object> map, StringBuffer sb) {
        Set<String> set = map.keySet();
        for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
            String key = (String) it.next();
            Object value = map.get(key);
            if (null == value)
                value = "";
            if (value instanceof List<?>) {
                List<Object> list = (List<Object>) map.get(key);
                sb.append("<" + key + ">");
                for (int i = 0; i < list.size(); i++) {
                    Map<String, Object> hm = (Map<String, Object>) list.get(i);
                    map2Element(hm, sb);
                }
                sb.append("</" + key + ">");
            } else {
                if (value instanceof Map<?, ?>) {
                    sb.append("<" + key + ">");
                    map2Element((Map<String, Object>) value, sb);
                    sb.append("</" + key + ">");
                } else {
                    sb.append("<" + key + ">" + value + "</" + key + ">");
                }

            }
        }
    }

    public static String map2Element(Map<String, Object> map) {
        StringBuffer sb = new StringBuffer();
        map2Element(map, sb);
        return sb.toString();

    }

    /**
     * 将多条记录信息Map后存入List
     *
     * @param
     * @return
     */
    public static List<Map> xml2List(List<Element> elements) {
        List list = new ArrayList();
        for (Element element : elements) {
            list.add(xml2Map(element));
        }
        return list;
    }


    /**
     * 从XML中解析出提交的表名
     *
     * @param e
     * @return
     */
    public static String getTableName(Element e) throws Exception {
        Element body = e.element("header").element("dataSet");
        return body.getTextTrim();
    }

    /**
     * 测试方法
     *
     * @param
     * @throws
     */
    public static void main(String[] xx) throws Exception {

        String data = "0.1111";

        System.out.println(isDate("2018-12-06aa"));

    }

    public static boolean isDate(String strDate) {
        Pattern pattern = Pattern
                .compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?(\\.[0-9]{1,3})?$");
        Matcher m = pattern.matcher(strDate);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

}